using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SunshineFarmWord
{
    public class PlayerSwimmingState : PlayerBaseStates
    {
        public PlayerSwimmingState(PlayerStateMachine playerStateMachine) : base(playerStateMachine)
        {
            if (stateMachine.ReusableData.PlayerStatesData.PlayerSwimmingData == null) stateMachine.ReusableData.PlayerStatesData.PlayerSwimmingData = new PlayerSwimmingData();

        }

        #region Base function

        public override void Enter()
        {
            base.Enter();

            InitReusableData();

            StartAnimation(stateMachine.Player.AnimationData.SwimmingParameterHash);
        }

        public override void Exit()
        {
            base.Exit();

            ClearResableData();

            StopAnimation(stateMachine.Player.AnimationData.SwimmingParameterHash);
        }

        public override void PhysicsUpdate()
        {
            base.PhysicsUpdate();
            Move();
        }
        #endregion


        #region Main Function

        //移动
        protected override void Move()
        {
            if (!stateMachine.ReusableData.IsSwimmingState) return;
            base.Move();
        }

        // 初始化数据
        private void InitReusableData()
        {
            stateMachine.ReusableData.IsSwimmingState = true;
            stateMachine.ReusableData.MovementSpeedModifier = stateMachine.ReusableData.PlayerStatesData.PlayerSwimmingData.SwimSpeed;
        }
        // 清理数据
        private void ClearResableData()
        {
            stateMachine.ReusableData.IsSwimmingState = false;
        }
        #endregion

        #region 碰撞检测

        // 碰撞离开
        protected override void SwimmingColliderExit2D(Collider2D collider)
        {
            base.SwimmingColliderExit2D(collider);
            // 有输入 切换 移动状态
            // 没有输入 切换 待机状态
            if (stateMachine.ReusableData.MovementInput != Vector2.zero)
            {
                stateMachine.ChangeState(stateMachine.WalkState);
            }
            else
            {
                stateMachine.ChangeState(stateMachine.IdlingState);
            }
        }
        #endregion
    }
}
